Task 1
# load packages
library(tidyverse)
library(RColorBrewer)
library(lubridate)
library(gganimate)
library(ggimage)
# read in datasets
agencies <- read_csv("agencies.csv")
launches <- read_csv("launches.csv")
# Make launch data frame with country, launch year, and number of launches per year for the graph with top 4 countries and the rest as "other"
launch_1 <- launches %>%
mutate(country = fct_collapse(state_code,
"Russia/USSR" = c("RU", "SU"),
"United States" = "US",
"China" = "CN",
"France" = "F"),
country = fct_lump(country, 4)) %>%
group_by(country, launch_year) %>%
count()
# Want to find the percent of success per country to use for dot size in gganimate
# make a dataframe with percent success per country
launch_2 <- launches %>%
select(state_code, category) %>%
mutate(country = fct_collapse(state_code,
"Russia/USSR" = c("RU", "SU"),
"United States" = "US",
"China" = "CN",
"France" = "F"),
country = fct_lump(country, 4)) %>%
group_by(country, category) %>%
count()
# number of successes per country from the counts in launch_2: C-289, F-284, USSR-3024, US-1585, Other-202
# use that dataframe to calculate the percent of successful launches by country
#first make a dataframe that just has total number of launches by country
launch_success <- launches %>%
mutate(country = fct_collapse(state_code,
"Russia/USSR" = c("RU", "SU"),
"United States" = "US",
"China" = "CN",
"France" = "F"),
country = fct_lump(country, 4)) %>%
group_by(country) %>%
count()
# add a column for number of successes using values found from counts in launch_2 dataframe
launch_success$number_success <- c(289, 284, 3024, 1585, 202)
# final dataframe that finds the percentage of success
success <- launch_success %>%
mutate(percent = number_success/n) %>%
select(country, percent)
# join data frames for final graph
launch_data <- left_join(launch_1,success, by="country")
# Read in rocket image for gganimate
rocket <- "rocket.png"
# Graph for year vs. counts by country, color = percent of successes per country
launch_graph <- ggplot(launch_data, aes(x=launch_year, y = n, group=country, color=percent)) +
geom_line() +
geom_image(aes(image = rocket)) +
scale_y_continuous(expand = c(0,0), limits = c(0,120), breaks = seq(0,120, by = 15)) +
scale_x_continuous(expand = c(0,0), limits = c(1955, 2020), breaks = seq(1955,2020, by=5)) +
labs(x="Launch Year", y = "Number of Launches") +
scale_color_distiller(palette = "YlGnBu", direction = 1,
name = "Percent of Successful\nLaunches by Country") +
theme_classic() +
geom_text(aes(label= country, color = percent), position = position_nudge(y = 0.04, x = 9),
size = 5) +
transition_reveal(country, launch_year)
## Warning: Ignoring unknown parameters: image_colour
launch_graph
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
## Warning: Removed 5 rows containing missing values (geom_text).
